package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Brand;
import cn.wolfcode.wms.domain.Product;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.query.ProductQueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.IProductService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.util.UploadUtil;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.Map;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService service;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private ServletContext ctx;

    @RequiredPermission("商品列表")
    @RequestMapping("list")
    public String list(Map map, @ModelAttribute("qo") ProductQueryObject qo) {
        map.put("brands", brandService.list());
        PageResult pageResult = service.query(qo);
        map.put("pageResult", pageResult);
        return "product/list";
    }
    @RequestMapping("productListView")
    public String productListView(Map map, @ModelAttribute("qo") ProductQueryObject qo) {
        qo.setPageSize(20);
        map.put("brands", brandService.list());
        PageResult pageResult = service.query(qo);
        map.put("pageResult", pageResult);
        return "product/productListView";
    }

    @RequiredPermission("商品编辑")
    @RequestMapping("input")
    public String input(Map map, Long id) {
        map.put("brands", brandService.list());
        if (id != null) {
            Product product = service.get(id);
            map.put("product", product);
        }
        return "product/input";
    }

    @RequiredPermission("商品保存或更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Product product, MultipartFile img) {
        Brand brand = brandService.get(product.getBrandId());
        product.setBrandName(brand.getName());
        if (img != null) {
            if (product.getId() != null && product.getImagePath() != null) {
                UploadUtil.deleteFile(ctx,product.getImagePath());
            }
            String imagePath = UploadUtil.upload(img, ctx.getRealPath("/upload"));
            product.setImagePath(imagePath);
        }
        if (product.getId() != null) {
            try {
                service.update(product);
            } catch (Exception e) {
                return JSONResult.mark(e.getMessage());
            }
        } else {
            try {
                service.save(product);
            } catch (Exception e) {
                return JSONResult.mark(e.getMessage());
            }
        }
        return JSONResult.success("操作成功");
    }

    @RequiredPermission("商品删除")
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id, String imagePath) {
        if (imagePath != null) {
            UploadUtil.deleteFile(ctx, imagePath);
        }
        if (id != null) {
            try {
                service.delete(id);
            } catch (Exception e) {
                return JSONResult.mark(e.getMessage());
            }
        }
        return JSONResult.success("删除成功");
    }
}
